package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UsersService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private QuestionApi questionApi; //问题

    @DubboReference
    private BlackListApi blackListApi; //黑名单

    @DubboReference
    private SettingsApi settingsApi; //开关

    //根据id查询用户资料
    public UserInfoVo findUserInfoById(Long userId) {
        //1、查询数据
        UserInfo info = userInfoApi.findById(userId);
        //2、将对象转化为vo
        UserInfoVo vo = new UserInfoVo();
        //企业中就是需要一个一个设置数据
        //教学：实体类和vo对象 （大部分的属性名和类型一致）
//        vo.setId(info.getId());
//        vo.setNickname(info.getNickname());
        BeanUtils.copyProperties(info,vo); //将参数一的同名同类型属性，设置到参数二对象中
        //不同的，需要手动处理
        if(info.getAge() != null ) {
            vo.setAge(info.getAge().toString());
        }
        //3、返回vo对象
        return vo;
    }

    //更新
    public void updateUserInfo(UserInfo info) {
        userInfoApi.update(info);
    }

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        //1、调用API，查询当前用户的陌生人问题(根据用户id查询)
        Question question = questionApi.findByUserId(userId);
        String txt = question == null? "你喜欢java吗？":question.getTxt();
        vo.setStrangerQuestion(txt);
        //2、调用API，查询通知的开关
        Settings settings = settingsApi.findByUserId(userId);
        if(settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        //3、获取当前用户的手机号码
        String mobile = UserHolder.getUser().getMobile();
        vo.setPhone(mobile);
        vo.setId(userId);
        //4、返回
        return vo;
    }

    /**
     * 设置陌生人问题
     *  保存或者更新
     */
    public void questions(String content) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //判断用户是否具有陌生人问题（查询）
        Question question = questionApi.findByUserId(userId);
        //问题对象不存在，保存
        if(question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }else {
            //问题对象存在，更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    //设置通知开关
    public void setting(Boolean like, Boolean pinglun, Boolean gonggao) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //根据用户id，查询通知开关对象
        Settings settings = settingsApi.findByUserId(userId);
        //如果对象不存在，保存
        if(settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settingsApi.save(settings);
        }else {
            //如果对象存在，更新
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settingsApi.update(settings);
        }
    }

    //查询黑名单用户数据（条件：用户id）
    public PageResult blacklist(Integer page, Integer pagesize) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用API根据用户id，分页查询黑名单数据  IPage<UserInfo>
        IPage<UserInfo> ipage = blackListApi.findByUserId(userId,page,pagesize);
        //3、将IPage对象转化成PageResult
        return new PageResult(page,pagesize,ipage.getTotal(),ipage.getRecords());
    }

    //移除黑名单
    public void delBlacklist(Long blackUserId) {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用API删除
        blackListApi.delete(userId,blackUserId);
    }
}
